package com.mallcai.bigdata.ladon.service.report.impl.handler.support;


import com.mallcai.bigdata.ladon.service.report.impl.ReportQueryParamHolder;

import java.util.Map;

/**
 * @author feixy
 * @version 1.0
 * @date 2019-07-20 17:25
 */
public class DynamicSqlRule implements IDynamicSqlRule {
    @Override
    public String toSql(Map<String, ReportQueryParamHolder> params) {
        return null;
    }

    //    private String content;
//
//    private String op;
//
//    private ExpressionHolder expressionHolder1;
//    private ExpressionHolder expressionHolder2;
//
//    public DynamicSqlRule(String content) {
//        this.content = content;
//        this.expressionHolder1 = new ExpressionHolder(extractValue(EXP1_PATTERN, content));
//        this.op = extractValue(OP_PATTERN, content);
//        this.expressionHolder2 = new ExpressionHolder(extractValue(EXP2_PATTERN, content));
//    }
//
//
//    private boolean test(Map<String, ReportQueryParamHolder> queryParams){
////        Object value1 = this.expressionHolder1.value;
////        Object value2 = this.expressionHolder2.value;
////        DataType dataType = DataType.STRING;
////        if(expressionHolder1.paramName != null && expressionHolder2.paramName != null){
////            ReportQueryParamHolder holder1 = checkParamNotNull(queryParams.get(expressionHolder1.paramName),expressionHolder1.paramName);
////            ReportQueryParamHolder holder2 = checkParamNotNull(queryParams.get(expressionHolder2.paramName), expressionHolder2.paramName);
////            // 两个参数的数据类型不一样,直接返回false
////            if (holder1.getDataType() != holder2.getDataType()){
////                return false;
////            }
////            dataType = holder1.getDataType();
////            value1 = ParamValueConverter.convertIfNecessary(holder1.getValue(), dataType);
////            value2 = ParamValueConverter.convertIfNecessary(holder2.getValue(), dataType);
////        } else if (expressionHolder1.paramName != null){
////            ReportQueryParamHolder holder1 = checkParamNotNull(queryParams.get(expressionHolder1.paramName),expressionHolder1.paramName);
////            dataType = holder1.getDataType();
////            value1 = ParamValueConverter.convertIfNecessary(holder1.getValue(), dataType);
////            value2 = ParamValueConverter.convert(expressionHolder2.value, dataType, expressionHolder2.isMulti);
////        } else if (expressionHolder2.paramName != null){
////            ReportQueryParamHolder holder2 = checkParamNotNull(queryParams.get(expressionHolder2.paramName),expressionHolder2.paramName);
////            dataType = holder2.getDataType();
////            value2 = ParamValueConverter.convertIfNecessary(holder2.getValue(), dataType);
////            value1 = ParamValueConverter.convert(expressionHolder1.value, dataType, expressionHolder1.isMulti);
////        }
////
////        IRuleOperator ruleOperator = RuleOperatorFactory.getRuleOperator(op, dataType);
////        return ruleOperator.test(value1, value2);
//        return true;
//    }
//
//    private <T> T checkParamNotNull(T obj, Object...args){
//        if (obj == null){
//            throw new BizException(String.format("非法的规则:参数[%s]未配置", args));
//        }
//        return obj;
//    }
//
//
//
//    private String extractValue(Pattern pattern,String content){
//        Matcher matcher = pattern.matcher(content);
//        if (matcher.find()){
//            return matcher.group(1).trim();
//        }
//        return null;
//    }
//
//    public static IDynamicSqlRule createRule(String ifContent) {
//        return new DynamicSqlRule(ifContent);
//    }
//
//    @Override
//    public String toSql(Map<String, ReportQueryParamHolder> queryParams) {
//        Pattern testPattern = test(queryParams) ? TEXT_PATTERN : ELSETEXT_PATTERN;
//        String ans = extractValue(testPattern, content);
//        return ans == null ? "" : ans;
//    }
//
//    static class ExpressionHolder{
//
//        private String value;
//
//        private String paramName;
//
//        private boolean isMulti = false;
//
//        public ExpressionHolder(String content) {
//            if(content != null){
//                this.paramName = getParamName(content);
//                if (this.paramName == null){
//                    this.value = getRealValue(content);
//                    this.isMulti = !value.equals(content);
//                }
//            }
//        }
//
//
//        /**
//         * 从exp中抽取参数名称
//         * @param exp
//         * @return
//         */
//        private String getParamName(String exp){
//            if(exp.startsWith(":")){
//                return exp;
//            }
//            Matcher matcher = SINGLE_PARAM_NAME_PATTERN.matcher(exp);
//            if (matcher.find()){
//                return matcher.group(1).trim();
//            }
//            return null;
//        }
//
//        private String getRealValue(String exp){
//            Matcher matcher = ARRAY_PATTERN.matcher(exp);
//            if (matcher.find()){
//                return matcher.group(1).trim();
//            }
//            return exp;
//        }
//    }

}
